package com.welyo.view.quizz.testwith {
	import com.welyo.model.quizz.BaseQuestionItemVO;
	import com.welyo.model.quizz.NumberQuestionItemVO;
	import com.welyo.model.quizz.TabItemVO;
	import com.welyo.model.QuizzModel;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author vu.doan
	 */
	public class TestWithNumber extends BaseTestWith {
		
		public function TestWithNumber() {
			timer = new Timer(2500);
			timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
			
			mcTitle.txtTitle.autoSize = TextFieldAutoSize.LEFT;
			mcTitle.txtTitleShadow.autoSize = TextFieldAutoSize.LEFT;
			mcDescription.txtDes.autoSize = TextFieldAutoSize.LEFT;
			arrOption = [mcA, mcB, mcC, mcD];
			for (var i:int = 0; i < arrOption.length; i++) {
				(arrOption[i] as MovieClip).mcEffect.mouseChildren = (arrOption[i] as MovieClip).mcEffect.mouseEnabled = false;
				(arrOption[i] as MovieClip).mcHit.buttonMode = true;
				(arrOption[i] as MovieClip).mcHit.addEventListener(MouseEvent.CLICK, onClickOptionHandler);
			}
		}
		
		override public function start(value:TabItemVO):void {
			totalMark = 0;
			
			data = value;
			currentQuestion = 0;
			randomArray(value.Questions);
			showQuestion(currentQuestion);
		}
		
		override public function hide():void {
			super.hide();
			timer.stop();
		}
		
		private function randomArray(arr:Array):void {
			for (var i:int = 0; i < arr.length; i++) {
				function sortItem():int {
					return Math.random() * i - 1;
				}
				arr.sort(sortItem);
			}
		}
		
		private function showQuestion(index:int):void {
			this.mouseChildren = this.mouseEnabled = true;
			var itm:NumberQuestionItemVO = data.Questions[index];
			
			mcTitle.txtTitle.text = QuizzModel.getIntance().TitleQuestion + " " + (index + 1);
			mcTitle.txtTitleShadow.text = QuizzModel.getIntance().TitleQuestion + " " + (index + 1);
			
			//mcDescription.txtDes.text = itm.Label;
			
			mcResult.txtResult.text = itm.Result;
			mcResult.txtResultShadow.text = itm.Result;
			mcResult.y = mcDescription.y + mcDescription.height - 4;
			
			for (var i:int = 0; i < arrOption.length; i++) {
				(arrOption[i] as MovieClip).gotoAndStop(1);
				(arrOption[i] as MovieClip).txtNumber.text = itm.Answers[i].label;
				(arrOption[i] as MovieClip).txtNumberShadow.text = itm.Answers[i].label;
			}
		}
		
		private function onClickOptionHandler(evt:MouseEvent):void {
			this.mouseChildren = this.mouseEnabled = false;
			var index:int = arrOption.indexOf(evt.currentTarget.parent);
			var itm:NumberQuestionItemVO = data.Questions[currentQuestion];
			
			if (itm.CorrectAnswer == itm.Answers[index].id) {
				totalMark += 1;
				QuizzModel.getIntance().TotalMark += 10;
				(evt.currentTarget.parent as MovieClip).gotoAndStop(2);
				QuizzModel.getIntance().correctAnswer();
			} else {
				(evt.currentTarget.parent as MovieClip).mcEffect.gotoAndPlay(2);
				QuizzModel.getIntance().wrongAnswer();
			}
			currentQuestion += 1;
			timer.start();
		}
		
		private function onTimerHandler(evt:TimerEvent):void {
			timer.stop();
			
			if (currentQuestion < max_question) {
				showQuestion(currentQuestion);
			} else {
				data.HasComplete = true;
				this.mouseChildren = this.mouseEnabled = false;
				QuizzModel.getIntance().completeTestWith();
				hide();
				
				if (totalMark >= 5) {
					trackingQuizz("win");
				} else {
					trackingQuizz("lose");
				}
			}
		}
		
		private function trackingQuizz(strValue:String):void {
			var request:URLRequest = new URLRequest("tracking.php");
			var param:URLVariables = new URLVariables();
			
			param.gameid = "quizz2";
			param.value = strValue;
			request.data = param;
			request.method = URLRequestMethod.POST;
			var ldr:URLLoader = new URLLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteTrackingHandler);
			ldr.addEventListener(IOErrorEvent.IO_ERROR, onErrorTrackingHandler);
			ldr.load(request);
		}
		
		private function onErrorTrackingHandler(evt:IOErrorEvent):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteTrackingHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorTrackingHandler);
		}
		
		private function onCompleteTrackingHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteTrackingHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorTrackingHandler);
		}
	}
}